package com.bxx.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bxx.common.dto.BlogDto;
import com.bxx.common.lang.Result;
import com.bxx.entity.Blog;
import com.bxx.service.BlogService;
import com.bxx.util.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2020-11-12
 */
@Api(value = "用户管理")
@RestController
public class BlogController {

    @Autowired
    BlogService blogService;

    /**
     * 博客列表
     * @param currentPage
     * @return
     */
    @ApiOperation(value = "博客列表")
    @GetMapping("/blogs")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "20") Integer pageSize){

        Page page = new Page(currentPage, pageSize);
        IPage pageData = blogService.page(page, new QueryWrapper<Blog>().orderByDesc("created"));

        return Result.succ(pageData);
    }

    /**
     * 博客详情
     * @param id
     * @return
     */
    @ApiOperation(value = "博客详情")
    @GetMapping("/blog/{id}")
    public Result detail(@PathVariable(name="id") Long id){
        Blog blog = blogService.getById(id);
        Assert.notNull(blog, "该博客已被删除");
        return Result.succ(blog);
    }

    @GetMapping("/blog/backList")
    @RequiresAuthentication
    public Result backList(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(defaultValue = "") String keyWord, @RequestParam(defaultValue = "created") String sortWord){

        Page<BlogDto> pageData = blogService.getBlogList(new Page<>(currentPage,pageSize), keyWord, sortWord);
        return Result.succ(pageData);
    }


    /**
     * 博客修改或新增
     * @param blog
     * @return
     */
    @ApiOperation(value = "博客修改")
    @RequiresAuthentication
    @PostMapping("/blog/edit")
    public Result edit(@Validated @RequestBody Blog blog){
        Blog temp = null;
        if(blog.getId() != null){
            temp = blogService.getById(blog.getId());
            Assert.isTrue(temp.getUserId() == ShiroUtil.getProfile().getId(), "没有权限编辑");
        }else{
            temp = new Blog();
            temp.setUserId(ShiroUtil.getProfile().getId());
            temp.setCreated(LocalDateTime.now());
            temp.setStatus(1);
        }

        BeanUtil.copyProperties(blog,temp,"id","userId","created","status");
        blogService.saveOrUpdate(temp);
        return Result.succ(null);
    }

    /**
     * 根据ID删除博客
     * @param data
     * @return
     */
    @PostMapping("/blog/delete")
    @RequiresAuthentication
    public Result delete(@Validated @RequestBody String data){
        JSONObject jsonObject = JSON.parseObject(data);
        Integer id = (Integer)jsonObject.get("id");
        blogService.removeById(id);
        return Result.succ(null);
    }


    /**
     * 根据类型查询列表
     * @param classId
     * @return
     */
    @GetMapping("/blog/class/{classId}")
    public Result getBlogsByClass(@PathVariable(name="classId") Long classId){
        System.out.println(classId);
        List<Blog> data = blogService.list(new QueryWrapper<Blog>().eq("type", classId));
        return Result.succ(data);
    }
}


